const http = require("http");
const fs = require("fs");
const path = require("path");
const { URL } = require("url");
http.createServer((req, res) => {
    // console.log(req.method)
    switch (req.method) {
        case "GET":
            getResource(req, res);
            break;
        case "POST":
            postData(req, res);
            break;
        case "DELETE":
            deleteData(req, res);
        case "PUT":
            updateData(req, res);
            break
    }
}).listen(3000);

function getResource(req, res) {
    let urlObj = new URL(req.url, "http://192.168.174.166:3000")
    let pageName = ""
    //获取请求地址的参数值（？之后 &连接）
    let id = urlObj.searchParams.get("id");
    switch (urlObj.pathname) {
        case "/":
            pageName = "02login.html";
            break;
        case "/list":
            pageName = "04list.html";
            break;
        case "/detail":
            res.end("this is detail page " + id);
            return;
        default:
            pageName = "404.html";
            break;
    }

    let filePath = path.join(__dirname, pageName);
    let fileContent = fs.readFileSync(filePath);
    res.end(fileContent);
}

function postData(req, res) {
    res.end("this is a post request")
}
console.log("server is listening 3000")